package cn.yunhe.crm.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import cn.yunhe.crm.annotation.SystemLog;
import cn.yunhe.crm.entity.Business;
import cn.yunhe.crm.entity.Organization;
import cn.yunhe.crm.service.BusinessService;
import cn.yunhe.crm.service.OrganizationService;


@Controller
public class BusinessController {
	@Autowired
	@Qualifier("BusinessService")
	private BusinessService bService;

	public void setbService(BusinessService bService) {
		this.bService = bService;
	}
	
	@Autowired
	@Qualifier("OrganizationService")
	private OrganizationService oService;
	
	public void setoService(OrganizationService oService) {
		this.oService = oService;
	}

	/**
	 * 获取所有的商机表
	 * @return
	 */
	@RequestMapping("/getBusinessList.do")
	public String getAllBusiness(HttpSession session){
		List<Business> list = bService.getList();
		session.setAttribute("busiList", list);
		return "businessinfo";
		
	}
	
	/**
	 * 跳转到新增页面
	 * @return
	 */
	@RequestMapping("/tochancemanage_add.do")
	public String toBusinessAdd(){
		return "chancemanage_add";
	}
	/**
	 * 自动补全获取机构信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getOrganizationList.do")
	public Object getOrgList(){
		Map map = new HashMap();
		List<Organization> list = oService.getOrgList();
		return JSONArray.toJSONString(list);
	}
	/**
	 * 添加商机
	 * @return
	 */
	@SystemLog(des="添加商机")
	@RequestMapping("/businessAdd.do")
	public String AddBusiness(Business business,HttpServletRequest req){
		String stime = req.getParameter("ssourcingtime");
		//转换采购时间
		Date date = null;
		if(stime!=null&&!"".equals(stime)){
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				date=sdf.parse(stime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			business.setSourcingtime(date);
		}
		
		business.setCreatitime(new Date());
		business.setDelstate(0);
		boolean r = bService.addBusiness(business);
		if(r){
			return "forward:getBusinessList.do";
		}else{
			return "forward:tochancemanage_add.do";
		}
	}
	/**
	 * 跳转到更新商机页面
	 * @return
	 */
	@RequestMapping("tobusinessinfoupdate.do")
	public String tobusinessUpdate(@RequestParam Integer id,HttpServletRequest req){
		Business business = bService.findBusinessById(id);
		//对采购时间进行转换
		if(business.getSourcingtime()!=null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String sourcingtime = sdf.format(business.getSourcingtime());
			req.setAttribute("sourcingtime", sourcingtime);
		}
		req.setAttribute("business", business);
		
		return "chancemanage_update";
	}
	/**
	 * 更新商机
	 * @return
	 */
	@SystemLog(des="更新商机")
	@RequestMapping("businessUpdate.do")
	public String businessUpdate(Business business,HttpServletRequest req){
		String stime = req.getParameter("ssourcingtime");
		//转换采购时间
		Date date = null;
		if(stime!=null&&!"".equals(stime)){
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				date=sdf.parse(stime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			business.setSourcingtime(date);
		}
		boolean r = bService.updateBusiness(business);
		if(r){
			return "forward:getBusinessList.do";
		}else{
			return "forward:tobusinessinfoupdate.do";
		}
	}
	/**
	 * 查看商机信息
	 * @param id
	 * @param req
	 * @return
	 */
	@RequestMapping("tobusinessinfodetail.do")
	public String tobusinessinfodetail(@RequestParam Integer id,HttpServletRequest req){
		Business business = bService.findBusinessById(id);
		//对采购时间进行转换
		if(business.getSourcingtime()!=null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String sourcingtime = sdf.format(business.getSourcingtime());
			req.setAttribute("sourcingtime", sourcingtime);
		}
		req.setAttribute("business", business);
		
		return "chancemanage_detail";
	}
	
	/**
	 * 删除商机
	 * @param id
	 * @return
	 */
	@SystemLog(des="删除商机")
	@RequestMapping("businessinfodel.do")
	public String businessinfodel(@RequestParam Integer id){
		
		boolean r = bService.delBusiness(id);
		return "forward:getBusinessList.do";
	}
}
